package phase01.module3.code.question4;

import java.util.Scanner;

public class StudentTest {
    private static final Database<Student> db = Database.getInstance();

    private static void insert(int id, String name, int age, String gender) throws Exception {
        Student s = new Student(id, name, age, gender);
        s = new Student(id, name, age, gender);
        db.insert(s);
    }

    private static void update(Student s) {
        db.update(s);
    }

    private static void delete(int id) {
        Student s = new Student();
        s.setId(id);
        db.delete(s);
    }

    private static Student find(int id) {
        Student s = new Student();
        s.setId(id);
        return db.find(s);
    }

    public static void main(String[] args) throws Exception {
        int select = 0;
        Student a = new Student(1, "a", 15, "F");
        Student b = new Student(2, "b", 15, "M");
        System.out.println(a.equals(b));
        Scanner sc = new Scanner(System.in);
        int id;
        String name;
        int age;
        String gender;
        Student s;
        do {
            System.out.println("请选择操作：");
            System.out.print("0.退出\t1.新建学生\n");
            System.out.print("2.更新学生\t3.删除学生\n");
            System.out.print("4.查找学生\t5.显示所有学生\n");
            System.out.println("=======================================================================================");
            select = sc.nextInt();
            switch (select) {
                case 1:
                    System.out.print("请输入学生信息(id 姓名 年龄 性别)：");
                    id = sc.nextInt();
                    name = sc.next();
                    age = sc.nextInt();
                    gender = sc.next();
                    insert(id, name, age, gender);
                    break;
                case 2:
                    System.out.print("请输入学生信息(id 姓名 年龄 性别)：");
                    id = sc.nextInt();
                    name = sc.next();
                    age = sc.nextInt();
                    gender = sc.next();
                    s = new Student(id, name, age, gender);
                    update(s);
                    break;
                case 3:
                    System.out.print("请输入学生ID：");
                    id = sc.nextInt();
                    delete(id);
                    break;
                case 4:
                    System.out.print("请输入学生ID：");
                    id = sc.nextInt();
                    s = find(id);
                    System.out.println("查找结果：" + s);
                    break;
                case 5:
                    db.show();
                    break;
                default:
                    break;
            }
        } while (select != 0);
    }
}
